package com.hqj.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.hqj.util.MyMatcher;
import com.hqj.util.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro的配置文件类  登录、密码、会话、授权
 */

@Configuration  //把类注解为配置类  就相当于spring的applicationcontext.xml的Bean管理配置
public class ShiroConfig {

    //注解为bean文件   相当于<bean id="credentialsMatcher" class=""></bean>
    @Bean("credentialsMatcher")
    public CredentialsMatcher credentialsMatcher(){
        return new MyMatcher();
    }

    //登录验证器
    @Bean("myRealm")
    public MyRealm myRealm(@Qualifier("credentialsMatcher") CredentialsMatcher credentialsMatcher){
        MyRealm myRealm = new MyRealm();
        myRealm.setCredentialsMatcher(credentialsMatcher);
        return myRealm;
    }

    //自定义一个sessionManager
    @Bean("sessionManager")
    public SessionManager sessionManager(){
        return new SesseionConfig();
    }



    //桥梁，主要是Realm的管理认证配置
    @Bean("securityManager")
    public SecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm){
        //声明默认的安全管理器
        DefaultWebSecurityManager defaultWebSecurityManager =
                new DefaultWebSecurityManager();
        //注入自定义的MyRealm
        defaultWebSecurityManager.setRealm(myRealm);
        //注入自定义的sessionManager
        defaultWebSecurityManager.setSessionManager(sessionManager());

        return defaultWebSecurityManager;
    }




    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean =
                new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //设置登录界面
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");

        //配置过滤器
        Map<String,String> map = new LinkedHashMap<>();
        /*anon：不需要进行认证
        * authc：进行认证
        * user:需要进行登录权限认证
        * logout：退出拦截
         */
        //配置退出  不需要写controller和方法，  自动销毁session跳回login
        map.put("/logout","logout");
        //配置静态文件不需要进行验证
        map.put("/static/**","anon");
        map.put("/login","anon");
        map.put("/css/**","anon");
        map.put("/js/**","anon");
        map.put("/font/**","anon");
        map.put("/img/**","anon");
        map.put("/getCodeImg","anon");
        map.put("/uc/toLogin","anon");
        //需要进行权限拦截
        map.put("/**","user");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean   //缺少这个方法，前端页面就无法使用shiro的标签
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
